local addon = CreateFrame"Frame"
local _G = getfenv(0)
local db

local slashHandler = function(msg)
	if msg == "unlock" then
		addon:SetAlpha(1)
		addon:EnableMouse(true)
		addon:SetPoint"CENTER"
		ChatFrame1:AddMessage("|cffff6000p|r|cffffff00Buff:|r Unlocked! Drag the green box to place the buff frames. Use '/pbuff lock' to lock.")
	elseif msg == "lock" then
		addon:SetAlpha(0)
		addon:EnableMouse(false)
		point1,_,point2,xOffS,yOffS = addon:GetPoint()
		db = {
			xOff = xOffS,
			yOff = yOffS,
			pOne = point1,
			pTwo = point2,
		}
		_G.pBuffDB = db
		ChatFrame1:AddMessage("|cffff6000p|r|cffffff00Buff:|r Locked! Changes saved.")
	elseif msg == "reset" then
		db = {
			xOff = 0,
			yOff = 0,
			pOne = "CENTER",
			pTwo = "CENTER",
		}
		_G.pBuffDB = db
		addon:SetPoint(db.pOne, UIParent, db.pTwo, db.xOff, db.yOff)
	else
		ChatFrame1:AddMessage("|cffff6000p|r|cffffff00Buff:|r Use '/pbuff unlock' to move.")
	end
end

addon:SetScript("OnEvent", function(self)
	if(event=="PLAYER_LOGIN") then
		self:SetParent(UIParent)
		self:SetFrameLevel(0)
		self:SetHeight(10)
		self:SetWidth(10)
		self:SetAlpha(0)

		self:SetMovable(true)
		self:SetBackdrop({
			bgFile ="Interface\\ChatFrame\\ChatFrameBackground", tileSize = 5,
			insets = {left = -2, right = -2, top = -2, bottom = -2},
			}) 
	    self:SetBackdropColor(0,1,0,.5) 

		self:SetScript("OnMouseDown", function(self) self:StartMoving() end)
		self:SetScript("OnMouseUp", function(self) self:StopMovingOrSizing() end)
	
		TemporaryEnchantFrame:SetParent(UIParent)
		TemporaryEnchantFrame:ClearAllPoints()
		TemporaryEnchantFrame:SetPoint("TOPRIGHT", self, "TOPRIGHT", -6, -6)
		TemporaryEnchantFrame.SetPoint = function() end

		TicketStatusFrame:Hide()
        TicketStatusFrame.Show = function() end
	elseif(event=="ADDON_LOADED") then
		db = _G.pBuffDB
		if(not db) then
			db = {
			xOff = -5,
			yOff = -137,
			pOne = "TOPRIGHT",
			pTwo = "TOPRIGHT",
			}
			_G.pBuffDB = db
			ChatFrame1:AddMessage("|cffff6000p|r|cffffff00Buff:|r First load! Use '/pbuff unlock' to move.")
		else
			self:SetPoint(db.pOne, UIParent, db.pTwo, db.xOff, db.yOff)
		end
    elseif(event=="PLAYER_AURAS_CHANGED") then
        local i
        i = 1
        while _G["BuffButton"..i] do addon:Update("BuffButton"..i) i = i + 1 end
        i = 1
        while _G["DebuffButton"..i] do addon:Update("DebuffButton"..i, true) i = i + 1 end
        i = 1
        while _G["TempEnchant"..i] do addon:Update("TempEnchant"..i, true) i = i + 1 end
    end
end)

function addon:Update(name, isDebuff)
	local border = _G[name.."Border"]
	local icon = _G[name.."Icon"]
	local frame = _G[name]

	local texture = frame:CreateTexture(nil, "OVERLAY")
	texture:SetParent(frame)
	texture:SetPoint("TOPLEFT", frame, 2, -2)
	texture:SetPoint("BOTTOMRIGHT", frame, -2, 2)
	texture:SetTexture"Interface\\AddOns\\pBuff\\border"
	texture:SetVertexColor(.25,.25,.25)

	icon:SetTexCoord(0.07,0.93,0.07,0.93)
	icon:ClearAllPoints()
	icon:SetPoint("TOPLEFT", frame, "TOPLEFT", 5, -5)
	icon:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -5, 5)

	if border then border:Hide() end
	if isDebuff then
		local r, g, b = border:GetVertexColor()
		texture:SetVertexColor(r,g,b)
	end
end

SecondsToTimeAbbrev = function(time)
	local hr, m, s, text
	if time <= 0 then text = ""
	elseif(time < 3600 and time > 40) then
		m = floor(time / 60)
		s = mod(time, 60)
		text = (m == 0 and format("|cffffffff%d", s)) or format("|cffffffff%d:%02d", m, s)
	elseif time < 60 then
		m = floor(time / 60)
		s = mod(time, 60)
		text = (m == 0 and format("|cffff0000%d", s))
	else
		hr = floor(time / 3600)
		m = floor(mod(time, 3600) / 60)
		text = format("%d:%2d", hr, m)
	end
	return text
end

addon:RegisterEvent"PLAYER_AURAS_CHANGED"
addon:RegisterEvent"ADDON_LOADED"
addon:RegisterEvent"PLAYER_LOGIN"

_G.SlashCmdList['PBUFF'] = slashHandler
_G.SLASH_PBUFF1 = '/pbuff'